var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_id:0,
    shop_id:0,
    goods_list:[],
    goods_id:0,
    page:1,
    page_size:10,
    is_on_sale:0,
    agent_type:0,
    hostimgs: app.globalData.hostimgs,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //初始化
  onLoad: function (options) {
    var that = this
    console.log("传参store_id=" + options.store_id)
    this.setData({
      store_id: options.store_id,
      agent_type: app.globalData.agent_type,

    })
    //查询商品集合
    goods_list(that,options.store_id,1,that.data.page_size)



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  //选货
  xuanhuo:function(e){
    var that = this;
    var goods_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;//下标
    var shop_id = app.globalData.shop_id;
    console.log(index)
    //选货方法
    xuanhuo(that, shop_id, goods_id,index) 
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      page:1,
    })
    //查询商品集合
    goods_list(that, that.data.store_id, 1, that.data.page_size)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    //查询商品集合
    goods_list(that, that.data.store_id, that.data.page, that.data.page_size)
  },

  //修改商品
  updategoods:function(e){
    var goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: 'updategoods?goods_id='+goods_id,
    })
  },
  //删除商品
  delgoods:function(e){
    var that = this
    var goods_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          delgoods(that, that.data.store_id, goods_id)//删除商品
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  //跳转到商品详情
  detailgoods:function(e){
    var that = this
    var goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: 'detail?goods_id='+goods_id,
    })
  },
  // 商品上架 下架
  goods_isshow:function(e){
    var that = this;
    var goods_id = e.currentTarget.dataset.id;

    //调用上架 下架接口
    wx.request({
      url: baseUrl +'weapp/Goods/on_sale.html',
      data: {
        goods_id: goods_id,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {'content-type': 'application/json'}, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        //成功提示
        if(res.data.code == 0){
          //下架
          wx.showToast({
            title: '成功下架',
          })
        }else{
          // 1 上架
          wx.showToast({
            title: '成功上架',
          })
        }

      },
      fail: function () {},
      complete: function () {}
    })

    //修改 goods_list 集合中 的字段值
    var goods_list = that.data.goods_list;
    for(var i=0;i<goods_list.length;i++){
      if(goods_list[i].goods_id == goods_id){
        if (goods_list[i].is_on_sale == 1){
          goods_list[i].is_on_sale = 0;
        }else{
          goods_list[i].is_on_sale = 1;
        }
      }
    }
    that.setData({
      goods_list:goods_list,
    })
  },

})


//查询 商品
function goods_list(that,store_id,page,page_size) {
  // wx.showLoading({
  //   title: '加载中...',
  // })
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_list.html',

    data: {
      store_id: store_id,
      user_id:app.globalData.user_id,
      page:page,
      page_size,page_size,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      
      if(page == 1){
        //第一次
        that.setData({
          goods_list: res.data,
          page: page + 1,
        })
      }else{

        //分页
        var goods_list = that.data.goods_list;
        for (var i = 0; i < res.data.length; i++) {
          goods_list = goods_list.push(res.data[i])
        }

        that.setData({
          goods_list: goods_list,
          page: page + 1,
        })
      }
      wx.hideLoading();


    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//删除 商品
function delgoods(that, store_id, goods_id) {

  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/delete_goods.html',
    data: {
      store_id: store_id,
      goods_id: goods_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == 0){
        wx.showToast({
          title: '删除成功',
        })
        that.setData({
          goods_list:[],
        })
        //查询商品集合
        goods_list(that, store_id, 1, that.data.page_size)
      }else{
        wx.showToast({
          title: '删除失败',
        })
      }



    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}


// 选货 选择当前商品
function xuanhuo(that, shop_id, goods_id,index) {
  if (shop_id == 0) {
    wx.showModal({
      title: '提示',
      content: '请先完善店铺资料',
      showCancel: false,

      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.navigateTo({
            url: '../dianpu/savedianpu',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  } else {


    //请求服务器
    wx.request({
      url: baseUrl + 'weapp/Goods/add_shop_goods.html',
      data: {
        shop_id: shop_id,
        goods_id: goods_id,

      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        if (res.data.code == -1) {
          //你已经添加过该商品啦！
          wx.showToast({
            title: '已添加该商品',
          })

        } else if (res.data.code == -2) {
          //当前商品不存在或已下架！
          wx.showToast({
            title: '商品不存在或已下架',
          })
        } else {
          wx.showToast({
            title: '选货成功',
            icon: 'success',
            duration: 2000
          })

          var goods_list = that.data.goods_list;
          goods_list[index].is_own = 1;
          that.setData({
            goods_list:goods_list,
          })
        }
      },
      fail: function () {
      },
      complete: function () {
        // complete
      }
    })
  }
}